using System;
using System.Collections.Generic;
using System.Text;

namespace Krilib.Bridges
{
    public interface IDebugReporter
    {
        /// <summary>
        /// Add report with error string
        /// </summary>
        /// <remarks>
        /// this report will treat as "Info" priority.
        /// </remarks>
        /// <param name="reportstr">reporting string</param>
        void AddReport(string reportstr);

        /// <summary>
        /// Add report with exception data
        /// </summary>
        /// <remarks>
        /// this report will treat as "Fatal" priority.
        /// </remarks>
        /// <param name="excp">exception data</param>
        void AddReport(Exception excp);

        /// <summary>
        /// Add report with debug report data
        /// </summary>
        /// <param name="report">debug report data</param>
        void AddReport(DebugReport report);
    }

    /// <summary>
    /// Debug report data class
    /// </summary>
    public class DebugReport
    {
        public bool IsExceptionReport
        {
            get { return exception != null; }
        }

        private Exception exception;
        public Exception Exception { get { return exception; } }

        private string message;
        public string Message { get { return message != null ? message : exception.Message; } }

        public enum Priorities { Note, Info, Warn, Fatal }
        private Priorities priority;
        public Priorities Priority { get { return priority; } }

        private DateTime datetime;
        public DateTime DateTime { get { return datetime; } }

        public DebugReport()
        {
            exception = null;
            message = null;
            priority = Priorities.Note;
            datetime = DateTime.Now;
        }

        public DebugReport(string msg)
            : this()
        {
            message = msg;
        }

        public DebugReport(string msg, Priorities p)
            : this(msg)
        {
            priority = p;
        }

        public DebugReport(Exception e)
            : this()
        {
            exception = e;
            priority = Priorities.Fatal;
        }

        public DebugReport(Exception e, Priorities p)
            : this(e)
        {
            priority = p;
        }

        public override string ToString()
        {
            //No Data
            if (exception == null && message == null) return "";
            StringBuilder ret = new StringBuilder(30);
            switch (priority)
            {
                case Priorities.Note:
                    ret.Append("#)");
                    break;
                case Priorities.Info:
                    ret.Append("i)");
                    break;
                case Priorities.Warn:
                    ret.Append("!>");
                    break;
                case Priorities.Fatal:
                    ret.Append("*>");
                    break;
            }
            if (IsExceptionReport)
                ret.Append(exception.ToString());
            else
                ret.Append(message);
            ret.AppendLine();
            ret.AppendLine(" - " + datetime.ToString());
            ret.AppendLine();
            return ret.ToString();
        }
    }

}
